<?php 
class Zen_Behavior_Weight extends Zen_Behavior_Model {
	
	public function beforeCreate($params){
		$params = $this->_prepareCreateUpdate($params);
		return $params;
	}
	
	public function beforeUpdate($params){
		$params = $this->_prepareCreateUpdate($params);
		return $params;
	}
	
	public function _prepareCreateUpdate($params){
		if (! isset($params['weight']) || ! $params['weight']) { 
			$table = $this->getModel()->getTable();
			$row = $table->fetchRow($table->select()->from(array($this->getModel()->getTableName()),array('count' => 'count(*)')));
			$count = $row['count'];
			$params['weight'] = ++$count;
		}
		return $params;
	}
	
}